//
//  DiscoverViewController.m
//  ProductB
//
//  Created by 田阳 on 16/8/5.
//  Copyright © 2016年 田阳. All rights reserved.
//

#import "DiscoverViewController.h"
#import "CustomTitleView.h"
//控制器
#import "RadioController.h"//广播
#import "RecommendController.h"//推荐
#import "CategoryController.h"//分类
#import "rankViewController.h"//榜单
#import "AnchorController.h"//主播

@interface DiscoverViewController ()<UIScrollViewDelegate>

@property(nonatomic,strong)UITableView *tableView;

@property(nonatomic,strong)UIScrollView *scrollView;
@property(nonatomic,strong)UIScrollView *titleScrollView;

@property(nonatomic,assign)NSInteger myIndex;
@end

@implementation DiscoverViewController

- (NSInteger)myIndex
{
    self.myIndex = 1;
    return _myIndex;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"发现";
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.translucent= NO;
    //创建label
    [self creatSomeView];
    [self creatFiveController];
}

//创建5个label(推荐，分类，广播，榜单，主播)控制五个界面
- (void)creatSomeView{
    NSArray *titleArray = @[@"推荐",@"分类",@"广播",@"榜单",@"主播"];
    CGFloat height = kScreenHeight / 20;
    
    for (NSInteger i = 0; i < titleArray.count; i++) {
        CustomTitleView *customTitleView = [[CustomTitleView alloc] initWithFrame:CGRectMake(kScreenWidth / 5 * i, 0, kScreenWidth / 5, height) action:@selector(tapAction:)];
        customTitleView.tag = i + 1;
        customTitleView.label.text = titleArray[i];
        if (i == 0) {
            customTitleView.label.textColor = [UIColor redColor];
            customTitleView.myView.backgroundColor = [UIColor redColor];
        }
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
        [customTitleView addGestureRecognizer:tap];
        [self.view addSubview:customTitleView];
    }
    
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0,kScreenHeight / 20, kScreenWidth, kScreenHeight - 64 - height)];
    _scrollView.contentSize = CGSizeMake(kScreenWidth * 5, 0);
    _scrollView.pagingEnabled = YES;
    _scrollView.delegate = self;
    [self.view addSubview:_scrollView];
}
//title点击方法
- (void)tapAction:(UITapGestureRecognizer *)tap{
    for (CustomTitleView *customView in self.view.subviews) {
        if ([customView isKindOfClass:[CustomTitleView class]]) {
            customView.label.textColor = [UIColor blackColor];
            customView.myView.backgroundColor = [UIColor clearColor];
        }
    }
    NSInteger index = tap.view.tag;
    CustomTitleView *temp = [self.view viewWithTag:index];
    temp.label.textColor = [UIColor redColor];
    temp.myView.backgroundColor = [UIColor redColor];
    [self.scrollView setContentOffset:CGPointMake((index - 1) * kScreenWidth, 0) animated:YES];
}
//创建5个控制器
- (void)creatFiveController{
    RecommendController *recommendVC = [[RecommendController alloc] init];
    recommendVC.view.frame = CGRectMake(0, 0, kScreenWidth, _scrollView.frame.size.height);
    [_scrollView addSubview:recommendVC.view];
    [self addChildViewController:recommendVC];
    
    CategoryController *categoryVC = [[CategoryController alloc] init];
    categoryVC.view.frame = CGRectMake(kScreenWidth, 0, kScreenWidth, _scrollView.frame.size.height);
    [_scrollView addSubview:categoryVC.view];
    [self addChildViewController:categoryVC];
    
    RadioController *radioVC = [[RadioController alloc] init];
    radioVC.view.frame = CGRectMake(kScreenWidth * 2, 0, kScreenWidth, _scrollView.frame.size.height);
    [_scrollView addSubview:radioVC.view];
    [self addChildViewController:radioVC];
    
    rankViewController *listVC = [[rankViewController alloc] init];
    listVC.view.frame = CGRectMake(kScreenWidth * 3, 0, kScreenWidth, _scrollView.frame.size.height);
    [_scrollView addSubview:listVC.view];
    [self addChildViewController:listVC];
    
    AnchorController *anchorVC = [[AnchorController alloc] init];
    anchorVC.view.frame = CGRectMake(kScreenWidth * 4, 0, kScreenWidth, _scrollView.frame.size.height);
    [_scrollView addSubview:anchorVC.view];
    [self addChildViewController:anchorVC];
}

#pragma mark ------titleScrollView Delegate方法
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    CGFloat offSetX = self.scrollView.contentOffset.x;
    NSInteger index = offSetX / kScreenWidth + 1;
    CustomTitleView *customView = [self.view viewWithTag:index];
    customView.myView.backgroundColor = [UIColor redColor];
    customView.label.textColor = [UIColor redColor];
    
    if (index == 5) {
        
    }else{
        CustomTitleView *customRight = [self.view viewWithTag:index + 1];
        customRight.myView.backgroundColor = [UIColor whiteColor];
        customRight.label.textColor = [UIColor blackColor];
    }
    
    if (index == 1) {
        return;
    }else{
        CustomTitleView *customleft = [self.view viewWithTag:index - 1];
        customleft.myView.backgroundColor = [UIColor whiteColor];
        customleft.label.textColor = [UIColor blackColor];
    }
    self.myIndex = scrollView.contentOffset.x / kScreenWidth + 1;
    
    
}

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    NSLog(@"111");
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
